<!-- 票券管理 -->
<template>
  <el-form ref="ruleFormRef" :model="ruleForm" inline class="ruleForm">
    <el-form-item label="司机名称：" prop="driverName">
      <el-input
        v-model="ruleForm.queryRo.driverName"
        style="width: 150px"
        clearable
        placeholder="请输入司机名称"
      />
    </el-form-item>
    <el-form-item label="站点名称：" prop="stationName">
      <el-input
        v-model="ruleForm.queryRo.stationName"
        style="width: 150px"
        clearable
        placeholder="请输入站点名称"
      />
    </el-form-item>
    <el-form-item label="设备唯一编号：" prop="deviceCode">
      <el-input
        v-model="ruleForm.queryRo.deviceCode"
        style="width: 150px"
        clearable
        placeholder="请输入设备编号"
      />
    </el-form-item>

    <el-form-item label="创建时间：" >
      <el-date-picker
        v-model="createTime"
        type="datetimerange"
        :picker-options="pickerOptions"
        range-separator="至"
        start-placeholder="开始时间"
        end-placeholder="结束时间"
        value-format="YYYY-MM-DD HH:mm:ss"
      />
    </el-form-item>

    <el-form-item>
      <el-button type="primary" @click="search">查 询</el-button>
    </el-form-item>
  </el-form>

  <el-table :data="tableList" style="width: 100%" v-loading="loading"  :show-summary="summary" :summary-method="getSummaries">
    <el-table-column prop="driverName" label="司机名称" align="center" />
    <el-table-column prop="deviceCode" label="设备唯一编号" align="center" />
    <el-table-column prop="" label="绑定时间" align="center" />
    <el-table-column prop="" label="解绑时间" align="center" />
    <el-table-column prop="" label="使用时长" align="center" />

    <el-table-column prop="electricUse" label="使用电量(kwh)" align="center" />
    <el-table-column prop="stationName" label="站点名称" align="center" />
    <el-table-column prop="createTime" label="创建时间" align="center" />

    <el-table-column label="操作" align="center">
      <template #default="scope">
        <el-button link @click="openDialog(scope.row)" type="primary">查看详情</el-button>
      </template>
    </el-table-column>
  </el-table>



  <footer class="pagination">
    <el-pagination
      v-model:currentPage="ruleForm.current"
      v-model:page-size="ruleForm.pageSize"
      :page-sizes="[10, 20, 30, 50]"
      layout="total, sizes, prev, pager, next, jumper"
      :total="total"
      @size-change="handleSizeChange"
      @current-change="handleCurrentChange"
      small
    />
  </footer>

  <driverComputeDetailDialog
      v-model:show="showDialog"
      :detail="detail"
      @openDialog="openDialog"
  ></driverComputeDetailDialog>
</template>

<script lang="ts" setup>
import { ref, reactive, onMounted } from 'vue'
import driverComputeDetailDialog from './components/driverComputeDetailDialog.vue'
import api from '@/api/apis/driverComputeApi'
const ruleForm = reactive({
  queryRo:{
    driverName:"",
    stationName:"",
    createTime:"",
    deviceCode:""
  },
  current: 1,
  pageSize: 50,
  searchCount: true
})

const createTime=ref<any>('')
//总数
const total = ref(0)
var summary = ref(false)
const getSummaries = (param)=>{
  const { columns, data } = param;
  const sums = [];
  columns.forEach((column, index) => {
    const values = data.map(item => {
      var list = ['electricUse']
      if(list.includes(column.property)){
        return Number(item[column.property])
      }
    });

    if (!values.every(value => isNaN(value))) {
      // console.log('值------',values)
      sums[index] = values.reduce((prev, curr) => {
        const value = Number(curr);
        if (!isNaN(value)) {
          return prev + curr;
        } else {
          return prev;
        }
      }, 0);
      sums[index] += ' (kwh)';
    } else {
      sums[index] = '';
    }
  });

  return sums
}
//搜索
const search = () => {
  summary.value = true
  ruleForm.current = 1
  getTableList()
}

//表格数据
const tableList = ref([])


//加载
const loading = ref(false)
onMounted(()=>{
  getTableList()
})

//获取表格数据
const getTableList = async () => {
  loading.value = true
  try {
    let obj = { ...ruleForm }
    obj.queryRo.createTime =createTime.value?createTime.value:undefined
    let {data} = await api.driverComputeListPage(obj)
    tableList.value= data.data.rows
    total.value= Number(data.data.total)

  } catch (error) {
    loading.value = false
  }
  loading.value = false
}
// //改变每页条数
const handleSizeChange = (val: number) => {
  ruleForm.current = 1
  ruleForm.pageSize = val
  getTableList()
}
//改变当前页数
const handleCurrentChange = (val: number) => {
  ruleForm.current = val
  getTableList()
}

//弹框开关
const showDialog = ref(false)
//详情id
const detail = ref()
//打开详情弹框
const openDialog = (val: string) => {
  detail.value = val
  showDialog.value = true
}
</script>

<style scoped lang="scss">
footer {
  display: flex;
  justify-content: center;
  margin-top: 15px;
}
</style>
